def convert_to_hex(string):
    prefix = string[:4]  # 获取前四个字符
    suffix = string[4:]  # 获取剩余部分

    hex_prefix = ''.join([hex(ord(c))[2:].upper() for c in prefix])  # 将前四个字符转换为十六进制

    result = hex_prefix + suffix  # 拼接十六进制前缀和后缀

    return result


def convert_from_hex(string):
    prefix = string[:8]  # 获取前八个字符
    suffix = string[8:]  # 获取剩余部分

    # 将十六进制前缀转换为相应的字符
    original_prefix = ''.join([chr(int(prefix[i:i+2], 16)) for i in range(0, len(prefix), 2)])

    result = original_prefix + suffix  # 拼接字符前缀和后缀

    return result


def detect_and_convert(string):
    hex_chars = set("0123456789ABCDEF")

    # 判断字符串是否为十六进制格式
    if all(c in hex_chars for c in string):
        # 如果是十六进制格式，则进行逆推还原
        return convert_from_hex(string)
    else:
        # 如果不是十六进制格式，则进行转换为十六进制
        return convert_to_hex(string)


# 测试示例
input_string = "5A544547CE428ED6"
output_string = detect_and_convert(input_string)
print(output_string)

input_string = "SKYW76C9EB4C"
output_string = detect_and_convert(input_string)
print(output_string)

input_string = "48575443CE428ED6"
output_string = detect_and_convert(input_string)
print(output_string)
